using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using ZSW.Framework.Utils;

namespace ZSW.Framework.EditorTools
{
    public class LanHuBuilder : EditorWindow
    {
        [MenuItem("Tools/UGUI/LanHu Builder")]
        private static void Invoke()
        {
            GetWindow<LanHuBuilder>("LanHuUGUIBuilder").Show();
        }

        private class Element
        {
            public string name;
            public string x;
            public string y;
            public string width;
            public string height;
        }

        private string path;


        private void OnGUI()
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label("切图文件夹路径：", GUILayout.Width(100f));
            EditorGUILayout.TextField(path);
            if (GUILayout.Button("浏览", GUILayout.Width(40f)))
            {
                //Assets相对路径
                path = EditorUtility.OpenFolderPanel("选择切图文件夹", "", "").Replace(Application.dataPath, "Assets");
            }
            GUILayout.EndHorizontal();

            if (Event.current.control && Event.current.keyCode == KeyCode.V && Event.current.type == EventType.KeyDown)
            {
                PasteImage();
            }
        }

        private void PasteImage()
        {
            Element element = new Element();
            string copy = GUIUtility.systemCopyBuffer;
            if (string.IsNullOrEmpty(copy))
            {
                ZSWFUtil_Debug.LogError<LanHuBuilder>("请拷贝蓝湖内的信息");
            }
            else
            {
                string[] copyValues = copy.Split("\n");
                if (copyValues.Length < 7)
                {
                    ZSWFUtil_Debug.LogError<LanHuBuilder>("拷贝的信息不完整");
                    return;
                }

                element.name = copyValues[0].Replace("图层", "").Trim();
                element.x = copyValues[2].Replace("px", "").Trim();
                element.y = copyValues[3].Replace("px", "").Trim();
                element.width = copyValues[5].Replace("px", "").Trim();
                element.height = copyValues[6].Replace("px", "").Trim();
            }
            var canvas = GameObject.FindObjectOfType<Canvas>();
            if (canvas == null)
            {
                var newCanvas = new GameObject("Canvas");
                newCanvas.AddComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
                var scaler = newCanvas.AddComponent<CanvasScaler>();
                scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
                scaler.referenceResolution = new Vector2(1920, 1080);
                scaler.matchWidthOrHeight = 0.5f;
                canvas = newCanvas.GetComponent<Canvas>();
            }

            Image image = new GameObject(element.name).AddComponent<Image>();
            image.transform.SetParent(canvas.transform, false);

            if (path == "")
            {
                ZSWFUtil_Debug.Log<LanHuBuilder>($"贴图路径为空");
            }
            else
            {
                string spritePath = path + "/" + element.name + "@2x.png";
                var obj = AssetDatabase.LoadAssetAtPath<Sprite>(spritePath);
                if (obj != null)
                {
                    image.sprite = obj;
                }
                else
                {
                    ZSWFUtil_Debug.Log<LanHuBuilder>($"未找到贴图 {spritePath}");
                }

                RectTransform rt = image.transform as RectTransform;

                float.TryParse(element.x, out float xValue);
                float.TryParse(element.y, out float yValue);
                float.TryParse(element.width, out float wValue);
                float.TryParse(element.height, out float hValue);

                rt.anchorMin = Vector2.one * 0.5f;
                rt.anchorMax = Vector2.one * 0.5f;
                rt.anchoredPosition = new Vector2(xValue - 960 + wValue / 2, -yValue + 540 - hValue / 2);
                rt.sizeDelta = new Vector2(wValue, hValue);

                ZSWFUtil_Debug.Log<LanHuBuilder>($"加载切图成功：{spritePath}");
            }
        }
    }
}